package com.yaodian100.ec.report.rank.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.generate.service.GenerateService;
import com.yaodian100.ec.rank.entity.ViewReportProductClick;
import com.yaodian100.ec.report.rank.service.ReportProductClickService;
import com.yaodian100.ec.report.rank.service.ViewReportProductClickService;

public class ReportProductClickAction extends ActionSupport implements Preparable {
	
	private static final String FIELD_NAME_CATEGORY_ID = "categoryId";

	private static final String RESULT_VIEW = "view";
	private static final String RESULT_AVAILABLE_CHILD_CATEGORY_OPTION_LIST = "availableChildCategoryOptionList";
	private static final String RESULT_PRODUCT_ROWS = "productRows";

	private static final long serialVersionUID = 1L;
	
	private List<ViewReportProductClick> viewReportProductClickList;
	private String topCategoryId;
	private String categoryId;
	private Category category;
	private List<Category> categoryRoot;
	private List<Category> categoryList;
	private List<String> productId;
	private List<String> deleteProductId;
	private List<String> deleteCategoryId;
	private boolean querySubmitted;
	private Integer retroDays;
	private boolean newRecords;
	private List<Boolean> newRecord;
	private List<String> selectedProductId;
	
	private boolean clearMessage = true;
	
	@Autowired
	private ViewReportProductClickService viewReportProductClickService;
	@Autowired
	private ReportProductClickService reportProductClickService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private GenerateService generateService;

	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	public String enter() {
		clearActionErrors();
		clearMessages();
		clearFieldErrors();
		
		setTopCategoryId(null);
		setCategoryId(null);
		setCategoryList(null);
		setViewReportProductClickList(null);
		setQuerySubmitted(false);
		return RESULT_VIEW;
	}
	
	public String view() {
		if (!this.clearMessage) {
			//判斷完後復原標記
			this.clearMessage = true;
		} else {
			clearActionErrors();
			clearMessages();
			clearFieldErrors();
		}
		
		if (!StringUtils.isBlank(this.topCategoryId)) {
			Long topCategoryId = Long.parseLong(this.topCategoryId);
			this.categoryList = getCategoryService().getAvailableChild(topCategoryId);
			this.viewReportProductClickList = null;
		}
		
		if (!querySubmitted) {
			return RESULT_VIEW;
		}
		
		if (StringUtils.isBlank(this.categoryId)) {
			return RESULT_VIEW;
		}
		
		int shortRetroDays = viewReportProductClickService.rankShortDays();
		int longRetroDays = viewReportProductClickService.rankLongDays();
		int minimumRecordCount = viewReportProductClickService.mininumRecordSize();
		
		List<ViewReportProductClick> viewReportProductClickList = viewReportProductClickService.getTopRank(categoryId, shortRetroDays);
		setRetroDays(shortRetroDays);
		if (viewReportProductClickList.size() < minimumRecordCount) {
			viewReportProductClickList = viewReportProductClickService.getTopRank(this.categoryId, longRetroDays);
			setRetroDays(longRetroDays);
		}
		setViewReportProductClickList(viewReportProductClickList);
		
		setNewRecords(false);
		
		return RESULT_VIEW;
	}
	
	public String save() throws CoreException, IOException {
		List<String> productIdList = getProductId();
		List<Boolean> newRecordMarkList = getNewRecord();
		int retroDays = getRetroDays().intValue();
		
		reportProductClickService.save(Long.valueOf(this.categoryId), productIdList, newRecordMarkList, retroDays, getDeleteProductId(), getDeleteCategoryId());
		generateService.generateCategoryTopRank(this.categoryId);
		
		clearActionErrors();
		clearMessages();
		clearFieldErrors();
		
		addActionMessage("储存成功");
		clearMessage = false;

		return view();
	}
	
	public String topCategorySelected() {
		if (StringUtils.isBlank(this.topCategoryId)) {
			return RESULT_VIEW;
		}
		Long topCategoryId = Long.parseLong(this.topCategoryId);
		this.categoryList = getCategoryService().getAvailableChild(topCategoryId);
		this.viewReportProductClickList = null;
		this.categoryId = null;
		this.querySubmitted = false;
		
		return RESULT_VIEW;
	}
	
	public String availableChildCategoryOptionList() {
		if (StringUtils.isBlank(this.topCategoryId)) {
			return RESULT_AVAILABLE_CHILD_CATEGORY_OPTION_LIST;
		}
		Long topCategoryId = Long.parseLong(this.topCategoryId);
		this.categoryList = getCategoryService().getAvailableChild(topCategoryId);
		
		return RESULT_AVAILABLE_CHILD_CATEGORY_OPTION_LIST;
	}
	
	public String productRows() {
		List<ViewReportProductClick> productRowList = new ArrayList<ViewReportProductClick>();
		for (String oneSelectedProductId: this.selectedProductId) {
			ViewReportProductClick oneRow = viewReportProductClickService.createNewRecordFromProductId(oneSelectedProductId);
			productRowList.add(oneRow);
		}
		setViewReportProductClickList(productRowList);
		setNewRecords(true);
		return RESULT_PRODUCT_ROWS;
	}
	
	@Override
	public void prepare() throws Exception {
		if (this.categoryRoot == null) {
			this.categoryRoot = categoryService.getChild(new Long(0));
		}
	}
	
	//========== getters & setters ==========

	public List<ViewReportProductClick> getViewReportProductClickList() {
		return this.viewReportProductClickList;
	}
	public void setViewReportProductClickList(List<ViewReportProductClick> viewReportProductClickList) {
		this.viewReportProductClickList = viewReportProductClickList;
	}
	
	public List<Category> getCategoryRoot() {
		return this.categoryRoot;
	}
	public void setCategoryRoot(List<Category> categoryRoot) {
		this.categoryRoot = categoryRoot;
	}

	public ViewReportProductClickService getViewReportProductClickService() {
		return this.viewReportProductClickService;
	}
	public void setViewReportProductClickService(ViewReportProductClickService viewReportProductClickService) {
		this.viewReportProductClickService = viewReportProductClickService;
	}

	public CategoryService getCategoryService() {
		return this.categoryService;
	}
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public String getTopCategoryId() {
		return this.topCategoryId;
	}
	public void setTopCategoryId(String topCategoryId) {
		this.topCategoryId = topCategoryId;
	}

	public String getCategoryId() {
		return this.categoryId;
	}
	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public boolean isQuerySubmitted() {
		return this.querySubmitted;
	}
	public void setQuerySubmitted(boolean querySubmitted) {
		this.querySubmitted = querySubmitted;
	}

	public List<Category> getCategoryList() {
		if (this.categoryList == null) {
			return new ArrayList<Category>();
		}
		return this.categoryList;
	}
	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public Category getCategory() {
		return this.category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}

	public List<String> getSelectedProductId() {
		return this.selectedProductId;
	}
	public void setSelectedProductId(List<String> selectedProductId) {
		this.selectedProductId = selectedProductId;
	}

	public Integer getRetroDays() {
		return this.retroDays;
	}
	public void setRetroDays(Integer retroDays) {
		this.retroDays = retroDays;
	}

	public boolean isNewRecords() {
		return this.newRecords;
	}
	public void setNewRecords(boolean newRecords) {
		this.newRecords = newRecords;
	}

	public List<String> getProductId() {
		return this.productId;
	}
	public void setProductId(List<String> prductId) {
		this.productId = prductId;
	}

	public List<Boolean> getNewRecord() {
		return this.newRecord;
	}
	public void setNewRecord(List<Boolean> newRecord) {
		this.newRecord = newRecord;
	}

	public ReportProductClickService getReportProductClickService() {
		return this.reportProductClickService;
	}
	public void setReportProductClickService(ReportProductClickService reportProductClickService) {
		this.reportProductClickService = reportProductClickService;
	}

	public GenerateService getGenerateService() {
		return this.generateService;
	}
	public void setGenerateService(GenerateService generateService) {
		this.generateService = generateService;
	}

	public List<String> getDeleteProductId() {
		return this.deleteProductId;
	}
	public void setDeleteProductId(List<String> deleteProductId) {
		this.deleteProductId = deleteProductId;
	}

	public List<String> getDeleteCategoryId() {
		return this.deleteCategoryId;
	}
	public void setDeleteCategoryId(List<String> deleteCategoryId) {
		this.deleteCategoryId = deleteCategoryId;
	}
	
}
